package com.lagou.edu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ResumeController {

    @Autowired
    private ResumeDao resumeDao;


    @RequestMapping("/login")
    @ResponseBody
    public JSONObject login(HttpServletRequest request, String username, String password, Model model) {
        System.out.println("login  username === " + username);
        System.out.println("login  password === " + password);
        JSONObject res = new JSONObject();
        if (username.equals("admin") && password.equals("admin")) {
            System.out.println("redirect ------");
            res.put("code", 0);
            return res;
        }
        res.put("401", 0);
        res.put("message", "账号密码错误！");
        return res;
    }


    @RequestMapping("/resume")
    @ResponseBody
    public List<Resume> getList(HttpServletRequest request) {
        System.out.println("getList -----------------------");
        return resumeDao.findAll();
    }

    @RequestMapping(value = "/resume", method = {RequestMethod.POST})
    @ResponseBody
    public JSONObject addResume(HttpServletRequest request, @RequestBody JSONObject params) {
        System.out.println("addResume resume === " + params);
        JSONObject jsonObject = new JSONObject();
        try {
            resumeDao.save(JSON.parseObject(JSON.toJSONString(params), Resume.class));
            jsonObject.put("code", 0);
        }catch (Exception e) {
            jsonObject.put("code", 500);
            jsonObject.put("message", e.getMessage());
        }
        return jsonObject;
    }

    @RequestMapping(value = "/resume", method = {RequestMethod.PUT})
    @ResponseBody
    public JSONObject updateResume(HttpServletRequest request, @RequestBody JSONObject params) {
        System.out.println("updateResume resume === " + params);
        JSONObject jsonObject = new JSONObject();
        try {
            resumeDao.save(JSON.parseObject(JSON.toJSONString(params), Resume.class));
            jsonObject.put("code", 0);
        }catch (Exception e) {
            jsonObject.put("code", 500);
            jsonObject.put("message", e.getMessage());
        }
        return jsonObject;
    }

    @RequestMapping(value = "/resume/{id}", method = {RequestMethod.DELETE})
    @ResponseBody
    public JSONObject deleteResume(HttpServletRequest request, @PathVariable Integer id) {
        System.out.println("deleteResume id === " + id);
        JSONObject jsonObject = new JSONObject();
        try {
            resumeDao.deleteById(id.longValue());
            jsonObject.put("code", 0);
        }catch (Exception e) {
            jsonObject.put("code", 500);
            jsonObject.put("message", e.getMessage());
        }
        return jsonObject;
    }

}